package cn.mesmile.nacos.model;

import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2021/6/18 23:55
 * @Description:
 */
public class DateTest {

    static  List<ObjectTest> result = new ArrayList<ObjectTest>();

    static {
        result.add(new ObjectTest( DateUtil.parseDateTime("2021-7-7 12:24:32"),0));
        result.add(new ObjectTest( DateUtil.parseDateTime("2021-7-7 13:24:32"),1));
        result.add(new ObjectTest( DateUtil.parseDateTime("2021-7-7 14:24:32"),0));
        result.add(new ObjectTest( DateUtil.parseDateTime("2021-7-8 12:24:32"),0));
        result.add(new ObjectTest( DateUtil.parseDateTime("2021-7-8 15:24:32"),1));
        result.add(new ObjectTest( DateUtil.parseDateTime("2021-7-8 16:24:32"),1));
    }

    public static void main(String[] args) {

        Map<String, Long> collect = result.stream().collect(Collectors.groupingBy(objectTest -> {
            Date dateTime = objectTest.getDateTime();
            return DateUtil.formatDate(dateTime);
        }, Collectors.counting()));
        System.out.println("collect.toString() = " + collect.toString());


        Map<String, Map<Integer, Long>> doubleGrouping = result.stream().collect(
                Collectors.groupingBy(objectTest -> {
                        Date dateTime = objectTest.getDateTime();
                        return DateUtil.formatDate(dateTime);
                    },
                        Collectors.groupingBy(ObjectTest::getType, Collectors.counting())
                )
        );
        System.out.println("doubleGrouping.toString() = " + doubleGrouping.toString());

    }


    @Data
    static class ObjectTest{

        private Date dateTime;

        // 0 代替类型一  1代表类型二
        private Integer type;

        public ObjectTest (Date dateTime, Integer type){
            this.dateTime = dateTime;
            this.type = type;
        }
    }

}
